/**
 * @ClassName: SysNotifyTemplate
 * @Description:閭欢璁剧疆妯℃澘
 * @author 832
 * @date 2016骞�10鏈�18鏃�
 */
package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.sys.bean.SysNotifyTemplate;
import com.fzrskj.cloud.free.sys.utils.SysNotifyTemplateUtils;
import ms.core.consts.SysErr;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class SysNotifyTemplateService {
	
	/**
	 * 查看全部邮件模板
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String querEmailConfig(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int type = CJsonTool.getIntValue(p, "type", -1);
		int page =CJsonTool.getIntValue(p, "page", 0);
		int pageSize =CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);


		SysNotifyTemplateUtils sntUtils = new SysNotifyTemplateUtils();
		int rows = sntUtils.queryCount(type);
		String code = sntUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return XmlCodeParser.formatResultTips(false,code);
		}
		int pages = Math.round((rows+pageSize-1)/pageSize);
		List<SysNotifyTemplate> querEmailConfig = sntUtils.querEmailConfig(type,page,pageSize);
		code = sntUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS))
			return XmlCodeParser.formatResultTips(false, code);
		return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, rows, CJsonTool.listBeanToJson(querEmailConfig));
	}

	/**
	 * 根据id查询模板
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String querEmailById(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		SysNotifyTemplateUtils sysNotifyTemplateUtils = new SysNotifyTemplateUtils();
		SysNotifyTemplate sysNotifyTemplate = sysNotifyTemplateUtils.querEmailById(id);
		String code =sysNotifyTemplateUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS))
			return XmlCodeParser.formatResultTips(false, code);
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, CJsonTool.beanToJson(sysNotifyTemplate));
	}


	/**
	 * 更新模板
	 * @param req
	 * @param resp
	 * @param p={"tempName":"tempName","title":"title","content","content"}
	 * @param sid
	 * @return
	 */
	public String uptemailconfig(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String tempName = CJsonTool.getStrValue(p, "tempName", "");
		String title = CJsonTool.getStrValue(p, "title", "");
		String content = CJsonTool.getStrValue(p, "content", "");
		SysNotifyTemplateUtils sysNotifyTemplateUtils = new SysNotifyTemplateUtils();
		int ret = sysNotifyTemplateUtils.uptemailconfig(tempName, title, content);
		String code = sysNotifyTemplateUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return XmlCodeParser.formatResultTips(false, code);	
		}
		return XmlCodeParser.formatResultTips(ret>0, code);
	}
}
